var app = getApp();
const util = require('../../utils/util.js')
const date = require('../../utils/date.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isSearch: true,
    hotList: [],
    searchText: "",
    list: [],
    isFirst: true,
    historySearchs: [],
    page: 1,
    size: 10,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      historySearchs: (wx.getStorageSync('_historySearchs') || [])
    })
    this.getHotList();
  },
  // 加载热门搜索词 
  getHotList: function() {
    util.http(app.globalData.getHotSearchsUrl, 'GET', {}, this.porcessHotList);
  },
  porcessHotList: function(res) {
    this.setData({
      hotList: res.data.hotSearchs
    });
  },
  //   键盘搜索
  searchBtn: function(e) {
    this.setData({
      searchText: e.detail.value,
      isSearch: false,
      isFirst: true,
      page: 1
    })
    let data = {
      page: this.data.page,
      size: this.data.size,
      keyword: this.data.searchText
    }
    util.http(app.globalData.searchVideoList, 'POST', data, this.porcessSearchList);
    this.setStorage();
    wx.showNavigationBarLoading();
    wx.showLoading();
  },
  // 点击热门搜索
  hotTap: function(e) {
    this.setData({
      searchText: e.currentTarget.dataset.hotText,
      isSearch: false,
      isFirst: true,
      page: 1
    })
    let data = {
      page: this.data.page,
      size: this.data.size,
      keyword: this.data.searchText
    }
    util.http(app.globalData.searchVideoList, 'POST', data, this.porcessSearchList);
    this.setStorage();
    wx.showNavigationBarLoading();
    wx.showLoading();
  },
  porcessSearchList: function(res) {
    console.log(res);
    var totalList = [];
    var list = [];
    for (var ids in res.data.videos) {
      var detail = res.data.videos[ids];
      var temp = {
        id: detail.id,
        title: detail.title,
        imgUrl: detail.pic,
        hits: detail.playCount,
        duration: detail.duration
      }
      list.push(temp);
    }

    if (!this.data.isFirst) {
      if (list.length < 1) {
        wx.showToast({
          title: '没有更多数据了',
          icon: 'none',
          duration: 2000
        })
      }
      totalList = this.data.list.concat(list);
    } else {
      totalList = list;
      this.data.isFirst = false;
      if (list.length < 1) {
        wx.showToast({
          title: '没有找到相关视频',
          icon: 'none',
          duration: 2000
        })
      }
    }
    this.setData({
      list: totalList
    })
    wx.hideNavigationBarLoading();
    wx.hideLoading();
  },
  //取消按钮
  cancel: function() {
    this.setData({
      isSearch: true,
      searchText: '',
      page: 1
    });
  },
  //存储历史搜索词
  setStorage: function() {
    let historySearchs = wx.getStorageSync('_historySearchs') || []
    let isRepeat = false;
    for (let ids in historySearchs) {
      if (historySearchs[ids] == this.data.searchText) {
        isRepeat = true;
      }
    }
    if (!isRepeat) {
      if (historySearchs.length >= 3) {
        historySearchs.splice(0, 1);
      }
      historySearchs.push(this.data.searchText);
    }
    wx.setStorageSync('_historySearchs', historySearchs);
    this.setData({
      historySearchs: historySearchs
    })
  },
  remove: function(event) {
    let index = event.currentTarget.dataset.index;
    let Searchs = wx.getStorageSync('_historySearchs');
    if (index > 0) {
      historySearchs.splice(index - 1, 1);
    } else {
      Searchs.splice(0, 1);
    }
    wx.setStorageSync('_historySearchs', Searchs);
    this.setData({
      historySearchs: Searchs
    })
  },
  clear: function() {
    wx.removeStorageSync('_historySearchs');
    this.setData({
      historySearchs: []
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var page = this.data.page;
    page++;
    this.setData({
      page: page
    });
    let data = {
      page: this.data.page,
      size: this.data.size,
      keyword: this.data.searchText
    }
    util.http(app.globalData.searchVideoList, 'POST', data, this.porcessSearchList);
    wx.showNavigationBarLoading();
    wx.showLoading();
  },

  /**
   * 用户点击右上角分享
   
  onShareAppMessage: function() {

  }
  */
})